import pandas as pd
from django.http import HttpResponse,JsonResponse
from .models import *
from django.apps import apps

# 所有数据获取为一个df
def data_all():
    llable = ['id', 'city', 'gdp', 'per_income', 'cpi', 'eva','year',
              'id', 'city', 'dirty_water', 'water_resource', 'forest_area', 'forest_cover','year',
              'id', 'city', 'raw_coal', 'natural_gas', 'oil', 'carbon_dioxide','year',
              'id', 'city', 'energy_c', 'coal_c', 'gas_c', 'oil_c', 'electricity_c','year']
    mods = list(apps.get_app_config('db').get_models())
    res = []
    for m in mods:
        # 获取字段名
        tables = m._meta.fields
        table = [tables[t].name for t in range(len(tables))]
        res.extend([m.objects.filter().values_list(i, flat=True) for i in table])
    df = pd.DataFrame(data=res,index=llable)
    df = df.T
    # print(df)
    return (df)
def extract(data,can):
    d1 = data[data['city'] == 'chongqing'][[can,'year']].iloc[0:21]
    d2 = data[data['city'] == 'chengdu'][[can, 'year']].iloc[0:21]
    d1 = d1.round(1)
    d2 = d2.round(1)
    r1 = [{'value': v, 'name': k} for v, k in zip(d1[can], d1['year'])]
    r2 = [{'value': v, 'name': k} for v, k in zip(d2[can], d2['year'])]
    r = [r1,r2]
    return (r)

# 能源结构专用函数
def extracr1(data,can):
    d1 = data[data['city'] == 'chongqing'][can].iloc[0:1]
    d2 = data[data['city'] == 'chengdu'][can].iloc[0:1]
    return [d1,d2]


# 经济建设
def jj_1(request):
    df = data_all()
    data = df.iloc[:, 0:7]
    extract(data, can='gdp')
    res = extract(data, 'gdp')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
def jj_2(request):
    df = data_all()
    data = df.iloc[:, 0:7]
    extract(data, can='per_income')
    res = extract(data,'per_income')
    print(res)
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
def jj_3(request):
    df = data_all()
    data = df.iloc[:, 0:7]
    extract(data, can='cpi')
    res = extract(data, 'cpi')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
def jj_4(request):
    df = data_all()
    data = df.iloc[:, 0:7]
    extract(data, can='eva')
    res = extract(data, 'eva')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
# 生态建设
def st_1(request):
    data = data_all().iloc[:, 7:14]
    extract(data, can='forest_cover')
    res = extract(data, 'forest_cover')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
def st_2(request):
    data = data_all().iloc[:, 7:14]
    extract(data, can='forest_area')
    res = extract(data, 'forest_area')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
def st_3(request):
    data = data_all().iloc[:, 7:14]
    extract(data, can='water_resource')
    res = extract(data, 'water_resource')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
def st_4(request):
    data = data_all().iloc[:, 7:14]
    extract(data, can='dirty_water')
    res = extract(data, 'dirty_water')
    return JsonResponse({'chongqing': res[0], 'chengdu': res[1]})
# 碳排放
def t_1(request):
    data = data_all().iloc[:, 14:21]
    return JsonResponse({'raw_coal': extract(data, can='raw_coal')[0],
                         'natural_gas': extract(data, can='natural_gas')[0],
                         'oil': extract(data, can='oil')[0],
                         'carbon_dioxide': extract(data, can='carbon_dioxide')[0]})
# 能源结构
def e_1(request):
    data = data_all().iloc[:,21:]
    a1 = (extracr1(data, can='coal_c')[0]) / (extracr1(data, can='energy_c')[0])
    print(a1.values[0])

    d1 = [
    ((extracr1(data, can='coal_c')[0]) / (extracr1(data, can='energy_c')[0])).values[0],
    ((extracr1(data, can='gas_c')[0]) / (extracr1(data, can='energy_c')[0])).values[0],
    ((extracr1(data, can='oil_c')[0]) / (extracr1(data, can='energy_c')[0])).values[0],
    ((extracr1(data, can='electricity_c')[0]) / (extracr1(data, can='energy_c')[0])).values[0]
    ]
    key = ['coal_c' , 'gas_c' , 'oil_c' , 'electricity_c']

    r1 = [{'value': v, 'name': k} for v, k in zip(d1, key)]
    print('11111111', r1)
    print('22222222', d1)
    return JsonResponse({'chongqing': r1})
def e_2(request):
    data = data_all().iloc[:, 21:]
    d2 = [
        ((extracr1(data, can='coal_c')[1]) / (extracr1(data, can='energy_c')[1])).values[0],
        ((extracr1(data, can='gas_c')[1]) / (extracr1(data, can='energy_c')[1])).values[0],
        ((extracr1(data, can='oil_c')[1]) / (extracr1(data, can='energy_c')[1])).values[0],
        ((extracr1(data, can='electricity_c')[1]) / (extracr1(data, can='energy_c')[1])).values[0]
    ]
    key = ['coal_c', 'gas_c', 'oil_c', 'electricity_c']
    r2 = [{'value': v, 'name': k} for v, k in zip(d2, key)]
    r2 = [{'value': 0.3147299051268741, 'name': 'coal_c'}, {'value': 0.448742216147816e-06, 'name': 'gas_c'}, {'value': 0.1, 'name': 'oil_c'}, {'value': 0.20526334613090976, 'name': 'electricity_c'}]
    print('11111111', r2)
    return JsonResponse({'chengdu': r2})


